﻿using DFGK.IService;
using DFGK.Service;
using DFGK_WMS.Common;
using DFGK_WMS.Model.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace DFGK_WMS.WebAPI.Controllers.权限管理
{
    /// <summary>
    /// 角色
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [ApiExplorerSettings(GroupName = "RBAC")]
    public class RoleController : BaseController
    {
        private readonly IRoleService roleService;
        public RoleController(IWebHostEnvironment environment, IRoleService roleService) : base(environment)
        {
            this.roleService = roleService;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddRole(Role m)
        {
            if (string.IsNullOrEmpty(m.RoleCode))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message = string.Empty;
            m.CreateUserId = base.BaseGetNowUserId();
            m.CreateUserName = base.BaseGetNowUserName();
            bool result = roleService.AddRole(m, out message);

            if (result)
            {
                return Ok(BaseResult.Result(result, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "添加失败！" + message, "200"));
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelRole(long rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = roleService.DelRole(rid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdRole(Role m)
        {
            if (string.IsNullOrEmpty(m.RoleCode))
            {
                return Ok(BaseResult.Result("400", "必填项不能为空！"));
            }
            string message = string.Empty;
            m.ModifyUserId = base.BaseGetNowUserId();
            m.ModifyUserName = base.BaseGetNowUserName();
            bool result = roleService.UpdRole(m, out message);

            if (result)
            {
                return Ok(BaseResult.Result(result, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(result, "修改失败！" + message, "500"));
        }

        /// <summary>
        /// 根据主键获取角色
        /// </summary>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult RoleById(long rid)
        {
            if (rid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var obj = roleService.GetRoleById(rid);
            if (obj != null)
            {
                return Ok(BaseResult.Result(obj, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！", "500"));
        }

        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <param name="rnam"></param>
        /// <param name="state"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleList(string? rnam = "", int state = 3, int pageIndex = 1, int pageSize = 5)
        {
            if (pageIndex <= 0 || pageSize <= 0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var rlist = roleService.GetRoleList(rnam, state, pageIndex, pageSize, out totalCount);

            if (rlist!=null)
            {
                return Ok(BaseResult.Result(rlist, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }

        /// <summary>
        /// 权限分配列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetRoleMenuActionList()
        {
            var list = roleService.GetRoleMenuActionList();
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", 0));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
        }
    }
}
